{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "971fbd65",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.font_manager import FontManager\n",
    "\n",
    "# 检测系统中可用的中文字体\n",
    "def get_available_chinese_fonts():\n",
    "    font_manager = FontManager()\n",
    "    chinese_fonts = []\n",
    "    for font in font_manager.ttflist:\n",
    "        font_name_lower = font.name.lower()\n",
    "        if ('china' in font_name_lower or 'chinese' in font_name_lower or \n",
    "            'sim' in font_name_lower or 'hei' in font_name_lower or\n",
    "            'song' in font_name_lower or 'ming' in font_name_lower):\n",
    "            chinese_fonts.append(font.name)\n",
    "    return list(set(chinese_fonts))  # 去重\n",
    "\n",
    "# 获取可用中文字体列表\n",
    "available_fonts = get_available_chinese_fonts()\n",
    "\n",
    "# 如果有可用中文字体，设置第一个可用字体，否则添加通用字体列表\n",
    "if available_fonts:\n",
    "    plt.rcParams[\"font.family\"] = [available_fonts[0]]\n",
    "else:\n",
    "    # 通用中文字体列表，覆盖不同系统\n",
    "    plt.rcParams[\"font.family\"] = [\"SimHei\", \"WenQuanYi Micro Hei\", \"Heiti TC\", \n",
    "                                   \"Microsoft YaHei\", \"SimSun\", \"NSimSun\",\n",
    "                                   \"PingFang SC\", \"Hiragino Sans GB\"]\n",
    "\n",
    "plt.rcParams[\"axes.unicode_minus\"] = False  # 解决符号显示问题\n",
    "\n",
    "# 1. 生成模拟数据\n",
    "np.random.seed(0)\n",
    "x = np.linspace(0, 10, 100)\n",
    "y = 2 * x + 5 + np.random.randn(100) * 2\n",
    "\n",
    "# 2. 实现线性回归模型\n",
    "X = np.vstack([x, np.ones(len(x))]).T\n",
    "w = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)\n",
    "slope, intercept = w\n",
    "\n",
    "# 3. 预测\n",
    "y_pred = slope * x + intercept\n",
    "\n",
    "# 4. 可视化结果\n",
    "plt.figure(figsize=(10, 6))\n",
    "plt.scatter(x, y, alpha=0.6, label='原始数据')\n",
    "plt.plot(x, y_pred, 'r-', linewidth=2, label=f'拟合直线: y={slope:.2f}x+{intercept:.2f}')\n",
    "plt.xlabel('x轴')\n",
    "plt.ylabel('y轴')\n",
    "plt.title('线性回归拟合示例')\n",
    "plt.legend()\n",
    "plt.grid(True, linestyle='--', alpha=0.7)\n",
    "\n",
    "# 打印当前使用的字体，帮助调试\n",
    "print(f\"当前使用的字体: {plt.rcParams['font.family']}\")\n",
    "plt.show()\n",
    "\n",
    "# 5. 预测示例\n",
    "def predict(new_x):\n",
    "    return slope * new_x + intercept\n",
    "\n",
    "test_x = [2.5, 7.8, 11.2]\n",
    "test_y = [predict(x) for x in test_x]\n",
    "print(\"预测结果:\")\n",
    "for xi, yi in zip(test_x, test_y):\n",
    "    print(f\"x={xi}, 预测y={yi:.4f}\")\n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ml-course",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
